<?php 
/*
 * NetfilterEyes - 2008 - Anouar Belkacem
 * This code is a modified copy of the FirewallEyes project source code
 *  
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
 
session_start();

if( isset($_SESSION['logfile2display'])) {
    $logfile=$_SESSION["logfile2display"] ;    
} 
  
include("configuration.php");
include("include.php");

// authentification check
authenticationCheck();

// Date in the past
header("Expires: Mon, 26 Jul 2004 00:00:00 GMT");

// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
 
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

// HTTP/1.0
header("Pragma: no-cache");

set_time_limit (300);

// GET INPUT
// log file, get input or first logfile
$configurationVars=Array("resolvService","exactSearch","displayedLines","criteria");
foreach($configurationVars as $confVarName) {
	${$confVarName} = $_SESSION[$confVarName] ; 
}

// init
$lineCount=0;
$indexForAction=getIndexForColumn("action",$logFields);
$indexForProtocol=getIndexForColumn("protocol",$logFields);

$maxWidth=0;
for($i=0; $i<count($logFields); $i++) {
	$maxWidth+=$logFields[$i][2];
}

    if  ( ! isset( $logfile ) ) {
      	die("Please enter a logfile path");
    }
		if(!file_exists ($logfile)) {
			die("unable to read log file : $logfile");
		}
		if(!is_readable ($logfile)) {
			die("No read access to log file : $logfile");
		}

		$fd = fopen ($logfile, "r");
		fseek($fd,0,SEEK_END); 
		
		while ( ftell($fd) > 0 ) {
			$line = fgetrs($fd) ;
			if(preg_match($detectLine, $line)) { // it's a firewall line
				if(preg_match($LineRegExp, $line, $infoTab)) {
					// resolv dns/services
					$infoTabOriginal=null;
					resolvAll();
					// Apply search array
					if(criteriaMatches($criteria,$logFields,$infoTab,$exactSearch)) {
						$lineCount++;
						$nb=($nb==1 ? 2 : 1); // for alternate display
						// line display
						?>
						<table class="<?=$infoTab[$indexForAction]?>" border="0" cellpadding="0" cellspacing="0" width="<?=$maxWidth?>">
            <tr class="line<?=$nb?>">
							<?php
						for($i=0; $i<count($logFields); $i++) 
						{ 
							?>
							  <td title="<?=($infoTabOriginal[$logFields[$i][1]] ? $infoTabOriginal[$logFields[$i][1]]." - " : "")?><?=$infoTab[$logFields[$i][1]]?>">
                <span class="tabCell" style="width: <?=$logFields[$i][2]?>px">
									<?php
									if($logFields[$i][4]) {
										?>
										<a href="index.php?info=1&type=<?=urlencode($logFields[$i][4])?>&p1=<?=urlencode($infoTab[$logFields[$i][1]])?>" title="informations"><img src="images/info.gif" width="15" height="15" border="0" align="absmiddle"></a>
										<?php
									}
									
									?>
								  &nbsp;<?=$infoTab[$logFields[$i][1]]?>
								</span>
                </td>
						<?php 
						}?>
            </tr>
            </table>
						<?php
						flush();
					}

				}
			}
			if($lineCount>=$displayedLines) break;
		}
		// close file
		fclose ($fd);
		?>